package com.yanqu.road.logic.activity.operamask;

import com.yanqu.road.entity.activity.operamask.OperaMaskSceneRank;
import com.yanqu.road.logic.bussiness.activity.OperaMaskBusiness;
import com.yanqu.road.logic.sort.OperaMaskSceneRankSort;
import com.yanqu.road.logic.sort.OperaMaskSceneSkValueRankSort;

import java.util.Comparator;
import java.util.LinkedList;

public class OperaMaskSceneRankListModel {

    private int activityId;
    private int sceneId;
    private int maxRank;
    private LinkedList<OperaMaskSceneRank> rankList = new LinkedList<>();
    private Comparator<OperaMaskSceneRank> rankSort = null;
    private int type;

    private boolean needSave = false;

    public OperaMaskSceneRankListModel(int activityId, int type, int sceneId, int maxRank, LinkedList<OperaMaskSceneRank> rankList) {
        this.activityId = activityId;
        this.sceneId = sceneId;
        this.maxRank = maxRank;
        this.rankList = rankList;
        this.type = type;
        if (type == 0) {
            rankSort = new OperaMaskSceneRankSort();
        } else {
            rankSort = new OperaMaskSceneSkValueRankSort();
        }
        rankList.sort(rankSort);
    }

    public void rankChange(OperaMaskSceneRank userRank) {
        synchronized (this) {

            boolean change = false;

            for (OperaMaskSceneRank tmpRank : rankList) {
                if (tmpRank.getUserId() == userRank.getUserId()) {
                    replaceRank(tmpRank, userRank);
                    change = true;
                    break;
                }
            }

            if (!change) {
                if (rankList.size() < maxRank) {
                    rankList.add(userRank);
                    change = true;
                } else {
                    OperaMaskSceneRank last = rankList.getLast();
                    if (rankSort.compare(userRank, last) < 0) {
                        replaceRank(last, userRank);
                        change = true;
                    }
                }
            }

            if (change) {
                rankList.sort(rankSort);
                needSave = true;
            }
        }
    }

    public int getMyRank(long userId) {
        int rankNum = 0;
        for (OperaMaskSceneRank rank : new LinkedList<>(rankList)) {
            rankNum++;
            if (rank.getUserId() == userId) {
                return rankNum;
            }
        }
        return -1;
    }

    public LinkedList<OperaMaskSceneRank> getRankList() {
        return new LinkedList<>(rankList);
    }

    private void replaceRank(OperaMaskSceneRank oldRank, OperaMaskSceneRank newRank) {
        oldRank.setUserId(newRank.getUserId());
        oldRank.setScore(newRank.getScore());
        oldRank.setUseStep(newRank.getUseStep());
        oldRank.setSkillList(newRank.getSkillList());
        oldRank.setTime(System.currentTimeMillis());
        oldRank.setSingLevel(newRank.getSingLevel());
        oldRank.setSkillValue(newRank.getSkillValue());
    }

    public void save() {
        if (needSave) {
            OperaMaskBusiness.replaceOperaMaskSceneRank(new LinkedList<>(rankList));
            needSave = false;
        }
    }
}
